<?php
/**
 * Created by PhpStorm.
 * User: FDG296
 * Date: 2017/8/2
 * Time: 16:42
 */
namespace common\models;

use common\helps\tools;
use Yii;
use yii\db\ActiveRecord;

class GoodsCategory extends ActiveRecord{

	const USE_FLG_YES	= 1;		//启用
	const USE_FLG_NO	= 0;		//禁用

	const COUNT_DIST_NUM= 4;		//模板显示方式根据分类个数大于4和小于4区分

    public static function tableName(){
        return '{{%goods_category}}';
    }

    //根据id获取分类列表
    public function getCategoryListById($cate_id){
        $where = ['use_flg'=>self::USE_FLG_YES,'parent_id'=>$cate_id];
        $result = $this::find()->where($where)->asArray()->orderBy('sort asc')->all();
        return $result;
    }

    //根据id获取分类详情
    public function getCategoryById($cate_id,$cate_child_id = 0,$tag_id = 0){
        $where = ['use_flg'=>1,'id'=>$cate_id];
        $result = $this::find()->where($where)->asArray()->one();
        if(empty($tag_id) && !empty($cate_child_id)){
            $where = ['use_flg'=>1,'id'=>$cate_child_id];
            $cate_result = $this::find()->where($where)->asArray()->one();
            if(!empty($cate_result['big_img_url'])){
                $result['big_img_url'] = $cate_result['big_img_url'];
            }
        }
        return $result;
    }

    //导航
    public function getNavMenu(){

        $tag_model = new GoodsCateRelation();
        $br_relation_model = new GoodsCategoryBrRelation();
        $recommend_model = new CategoryRecommend();
        $where = ['use_flg'=>1];
        //分类
        $list = $this::find()->where($where)->select('id,category_name,parent_id,img_url,img_link,sort,nav_type')->orderBy('sort asc')->asArray()->all();

        //品类
        $tag_list = $tag_model->getTagList();

        //品牌
        $brand_list = $br_relation_model->getBrandList();

        //推荐
        $recommend_list = $recommend_model->getList();

        foreach($list as &$child){
            $child['tag_list'] = empty($tag_list[$child['id']]) ? array() : $tag_list[$child['id']];
        }

        $parent_list = array();
        foreach($list as $value){
            if($value['parent_id'] == 0){
                $parent_list[] = $value;
            }
        }
        foreach($parent_list as &$parent){
            foreach($list as $value){
                if($parent['id'] == $value['parent_id']){
                    $parent['child_list'][] = $value;
                }
            }
            $parent['brand_list'] = array_merge(empty($brand_list[$parent['id']]) ? array() : $brand_list[$parent['id']]);
            $parent['recommend_list'] = empty($recommend_list[$parent['id']]) ? array() : $recommend_list[$parent['id']];
			rsort($parent['brand_list']);

		}

        return $parent_list;
    }

	/**
	 * @param string $cate_id 分类ID  //加入缓存 TODO
	 * @param int $level	调用级别 1：1级分类 2：从二级分类开始
	 * @param int $is_get_group 是否调取品类 1：调取当前分类下的品类 0：不调取
	 * @return array
	 */
	public static function getCategoryChild($cate_id='',$level=1,$is_get_group=1){
		$info=[];						//去除二级分类下的三级分类

		if($level <3){					//当前分类下的子分类
			$info= (new static())->getCategoryListById($cate_id);
		}
		//如果是二级分类调用当前分类下的品类
		if($level ==3 &&  1==$is_get_group){
			$info =(new GoodsCateRelation())->getRelationListByCateIdNews($cate_id);
		}
		if($info){
			$level++;
			foreach($info as $key=>&$val){
				if($level ==3 &&  1==$is_get_group){
					//当前分类下的品类
					$val['child']= (new GoodsCateRelation())->getRelationListByCateIdNews($val['id']);
				}else{
					$val['child']= self::getCategoryChild($val['id'],$level,$is_get_group);
				}
			}

		}
		return $info;

	}
}